package com.wen.pay.controller;

import com.wen.pay.service.WxPayService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.GeneralSecurityException;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/pay")
public class PayController {
    @Resource
    private WxPayService wxPayService;


    @PostMapping("/native/{productId}")
    public String pay(@PathVariable Long productId) {
        log.info("发起支付请求");
        //返回支付二维码连接和订单号
        Map<String, Object> map = wxPayService.nativePay(productId);
        return "支付成功";
    }
    /**
     * 支付通知
     * 微信支付通过支付通知接口将用户支付成功消息通知给商户
     */
    @ApiOperation("支付通知")
    @PostMapping("/native/notify")
    public String nativeNotify(HttpServletRequest request, HttpServletResponse response) throws GeneralSecurityException {
            log.info("通知方法接收到消息！");
//        Gson gson = new Gson();
//        Map<String, String> map = new HashMap<>();//应答对象
//        //处理通知参数
//        String body = HttpUtils.readData(request);
//        Map<String, Object> bodyMap = gson.fromJson(body, HashMap.class);
//        log.info("支付通知的id ===> {}", bodyMap.get("id"));
//        log.info("支付通知的完整数据 ===> {}", body);
//        //TODO : 签名的验证
//        //TODO : 处理订单
//        wxPayService.processOrder(bodyMap);
//
//        //成功应答：成功应答必须为200或204，否则就是失败应答
//        response.setStatus(200);
//        map.put("code", "SUCCESS");
//        map.put("message", "成功");
        return "支付成功";
    }
}
